Skip to content

fix: Avoid act() re-entrant when dispatching events - #1468

Merged
eps1lon merged 3 commits into
testing-library:mainfrom
snowystinger:fix-nested-acts
Aug 27, 2026
Merged

fix: Avoid act() re-entrant when dispatching events#1468
eps1lon merged 3 commits into
testing-library:mainfrom
snowystinger:fix-nested-acts

Conversation

@snowystinger

Copy link
Copy Markdown
Contributor

What:

Closes #1231
And removes the need for testing-library/user-event#1283

Why:

testing-library/user-event#1283

In React, nested synchronous act calls can prematurely teardown the act queue creating console warnings.

How:

By tracking if we're already inside an act, we can avoid wrapping it in a new one. At the same time, we can keep the wrapper around all events, which is an expectation using testing library's configure

Checklist:

  • Documentation added to the N/A
    docs site
  • Tests
  • TypeScript definitions updated N/A
  • Ready to be merged

Originally I was going to make this change over in user event, we have two PRs to do it, however, it's really a React and Preact concern. Changing it in User Event would affect everyone. Angular testing library already wraps all events for a different reason and so removing an event from that callback wrapper could be unexpected or breaking. I would guess this change is also needed in the Preact testing library repo as well.

@codesandbox-ci

codesandbox-ci Bot commented Aug 24, 2026

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 191aa58:

Sandbox Source
react-testing-library-examples Configuration

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write a test showing the underlying issue instead of testing implementation details (nesting depth)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry about that, I was stumped on it for a little bit
I got there today though, just needed to sleep on it. done.

Copy link
Copy Markdown

There is now a React-only public-API reproduction of the underlying failure this PR is working around: react/react#37374

The minimized case removes Testing Library, user-event, Radix, and focus/blur behavior entirely. A re-entrant act() entered while an outer flushActQueue is running sees actScopeDepth === 0, takes the outermost-exit path, cannot flush because isFlushing is already true, and nulls actQueue mid-flush. The outer act() then returns with React updates still pending on the real scheduler.

That issue includes a standalone node repro.mjs, expected vs actual behavior, causal controls, and the queue trace. It may also provide the behavioral test case requested in this PR without relying on act-depth instrumentation.

@eps1lon eps1lon changed the title fix nested acts fix: Avoid act() re-entrant when dispatching events Aug 27, 2026
@eps1lon
eps1lon merged commit 20ce75f into testing-library:main Aug 27, 2026
9 checks passed
@github-actions

Copy link
Copy Markdown

馃帀 This PR is included in version 16.3.3 馃帀

The release is available on:

Your semantic-release bot 馃摝馃殌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

'event was not wrapped in act' warning with React 18, react-datepicker and user.type

3 participants